//
//  ChannelModel.m
//  
//
//  Created by admin on 17/2/28.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "ChannelModel.h"
#import "YYModel.h"
//#import "NSDictionary+Data.h"
#import "NetworkTool.h"

@implementation ChannelModel

+ (void)getNewsCateWithSuccBlock:(void(^)(NSArray *modelArr))succBlock{
    
    //    //1.获得NSUserDefaults文件
    //    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //    //3.读取文件
    //    NSDictionary *cateJson = [userDefaults objectForKey:@"cate"];
    //
    //    if (cateJson == nil){

    //            NSArray *dictArr =
    //    @[ @{
    //         @"title": @"我要祷告",
    //         @"id": @"0",
    //
    //         },@{
    //         @"title": @"我要见证",
    //         @"id": @"1",
    //          },@{
    //           @"title": @"祷告词",
    //           @"id": @"4",
    //           },@{
    //         @"title": @"精品祷告",
    //         @"id": @"2",
    //          },@{
    //         @"title": @"精品见证",
    //         @"id": @"3",
    //      }];
    NSArray *dictArr =
    @[ @{
           @"title": @"我要祷告",
           @"id": @"0",
           },@{
           @"title": @"我要见证",
           @"id": @"1",
           },@{
           @"title": @"祷告词",
           @"id": @"4",
           },];
    NSArray *modelArr = [NSArray yy_modelArrayWithClass:[ChannelModel class] json:dictArr];
    //            NSLog(@"modelArr = %@", modelArr);
    succBlock(modelArr);
    //            //1.获得NSUserDefaults文件
    //            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //            //2.存储到偏好设置
    //            [userDefaults setObject:dictArr forKey:@"cate"];
    //
    //    }else{
    //        // 获取tList对应的字典数组
    //        NSArray *dictArr = cateJson[@"data"];
    //        // 字典数组转模型数组
    //        NSArray *modelArr = [NSArray yy_modelArrayWithClass:[ChannelModel class] json:dictArr];
    //        succBlock(modelArr);
    //    }
    //
    //    [userDefaults setObject:nil forKey:@"cate"];

}
+ (void)getHomeCateWithSuccBlock:(void(^)(NSArray<ChannelModel *> *modelArr))succBlock{
    YYCache *yyCache=[YYCache cacheWithName:@"LCHomeChannelName"];
    [yyCache.diskCache setCostLimit:10*1024];//磁盘最大缓存开销
    [yyCache.diskCache setAutoTrimInterval:60*60*24*1*1];//设置磁盘lru动态清理频率 默认 60秒
    NSString *key= [NSString stringWithFormat:@"%@",@"keyHomeChannel"];

    NetworkTool *networkTool = [NetworkTool sharedInstance];

    NSDictionary *parameters = @{
                                 @"interface": @"get_home_label"
                                 };
    [networkTool POSTFuYinWithUrlString:http_shengjing input:parameters success:^(NSDictionary *dict) {

        NSArray *array2;
        if ([dict[@"status"] integerValue] == 1) {
            array2 = dict[@"data"];
        }
        NSArray *array = [NSArray yy_modelArrayWithClass:[ChannelModel class] json:array2];
        NSMutableArray *modelArray = [NSMutableArray arrayWithArray:array];

        ChannelModel *model = [[ChannelModel alloc] init];
        model.id = @"0";
        model.title = @"推荐";
        [modelArray insertObject:model atIndex:0];

        succBlock(modelArray.copy);

#pragma mark -  添加数据添加到yyCache里缓存下来  -
        NSMutableArray *cacheMutableArray = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < modelArray.count; i++) {
            ChannelModel *model = modelArray[i];
            NSDictionary *json = [model yy_modelToJSONObject];
            [cacheMutableArray addObject:json];
        }
        [yyCache setObject:cacheMutableArray forKey:key];//添加数据到yyCache里缓存下来


    } failure:^(NSError *error) {

        if ([yyCache containsObjectForKey:key]) {
            //根据key读取数据
            id vuale = [yyCache objectForKey:key];
            NSArray *array = [NSArray yy_modelArrayWithClass:[ChannelModel class] json:vuale];
            succBlock(array);
        }else{
            NSArray *array = [NSArray yy_modelArrayWithClass:[ChannelModel class] json:@[@{@"id":@"0",@"title":@"推荐"}]];
            succBlock(array);
        }

        NSLog(@"error = %@", error);
    }];

}

- (NSString *)description {
    return [NSString stringWithFormat:@"%@ %@%@",self.cate,self.id,self.title];
}

@end
